package com.aijk.web.admin.controller.h5;

import com.aijk.ehm.api.*;
import com.aijk.ehm.model.*;
import com.aijk.ehm.model.qo.EhmInfoEvaluateQo;
import com.aijk.ehm.model.qo.EhmResidentQo;
import com.aijk.ehm.model.vo.EhmResidentVo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.entity.MedNewsEx;
import com.alibaba.dubbo.remoting.exchange.Response;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("h5/news")
public class NewsH5Controller extends BaseController {
    @Autowired
    private EhmInfoApi ehmInfoApi;

    @Autowired
    private EhmInfoScanApi ehmInfoScanApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    @Autowired
    private EhmInfoClassifyApi ehmInfoClassifyApi;

    @Autowired
    private EhmInfoSrcApi ehmInfoSrcApi;

    @Autowired
    private EhmInfoEvaluateApi ehmInfoEvaluateApi;


    @RequestMapping(value = "browse/{id}")
    public Object feedback_resident(@PathVariable("id") Long id,Long rid, HttpServletRequest request, Model model) {

        EhmInfo news = ehmInfoApi.findOne(id);
        //资讯信息未找到
        if(news == null){
            model.addAttribute("errorMessage","该资讯信息已被删除或未发布");
            return "/error/errorMessage";
        }
        EhmResident userEx = new EhmResident();
        if(rid  != null ){
            userEx = ehmResidentApi.findOne(rid);
            if(userEx.getId() == null){
                model.addAttribute("errorMessage","用户不存在");
                return "/error/errorMessage";
            }
        }
        news.setScanNum(news.getScanNum() +1);
        ehmInfoApi.update(news);
//2017.10.20 浏览数暂时不用
        /*//浏览数加1
        news.setScanNum(news.getScanNum() +1);
        ehmInfoApi.update(news);
        //新增资讯信息浏览记录
        EhmInfoScan browser = new EhmInfoScan();
        browser.setNewsid(news.getId());
        browser.setCellphone(userEx.getCellphone());
        browser.setScanTime(new Date());
        browser.setSex(userEx.getSex() == null ? (short) 1 : userEx.getSex().byteValue());
        browser.setName(userEx.getTitle());
        browser.setAge((int) (userEx.getAge() == null ? 0 : userEx.getAge().byteValue()));
        browser.setUserId(userEx.getId());
        ehmInfoScanApi.save(browser);*/

        //图文方式
        if(news.getHrefType() == 1){
            MedNewsEx newsEx = JSONObject.parseObject(JSONObject.toJSONString(news), MedNewsEx.class);
            if(news.getClassifyId() != null && news.getClassifyId() > 0){
                EhmInfoClassify tags = ehmInfoClassifyApi.findOne(news.getClassifyId());
                if(tags != null){
                    newsEx.setCategory_string(tags.getTitle());
                }
            }
            if(news.getSrcId() != null && news.getSrcId() > 0){
                EhmInfoSrc source = ehmInfoSrcApi.findOne(news.getSrcId());
                if(source != null){
                    newsEx.setSource_string(source.getTitle());
                }
            }
            /*HashMap skey = new HashMap();
            skey.put("news_id",news.getId());
            skey.put("uid",uid);
            List<MedNewsPraiseRecord> records = praiseRecordService.findAll(skey);
            if (records != null && records.size() > 0) {//表示已经点过赞了
                model.addAttribute("good",true);
            }else{
                model.addAttribute("good",false);
            }*/
            /*ResidentDownload download = ResidentDownload.get(request);
            model.addAttribute("download",download);*/
            model.addAttribute("object",newsEx);
            model.addAttribute("newsId",id);
            model.addAttribute("uid",rid);
            return "/h5/news/browse";
        }
        //外链方式或者内链
        return "redirect:"+news.getHrefUrl();
    }


//2017.10.20评论暂时不用
    //**
    //*资讯评论分页
    //**
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    public @ResponseBody
    RetMsg comment(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw,
                                        @RequestParam(value = "start", defaultValue = "0") Integer start,
                                        @RequestParam(value = "length", defaultValue = "20") Integer length, Long nid, Model model,
                                        ServletRequest servletRequest) {
        draw += 1;

        EhmInfoEvaluateQo ehmInfoEvaluateQo = new EhmInfoEvaluateQo();
        ehmInfoEvaluateQo.setNewsid(nid);
        ehmInfoEvaluateQo.setSortway("evaluate_time desc");

        List<EhmInfoEvaluate> evaluateList = ehmInfoEvaluateApi.list(ehmInfoEvaluateQo);
        if(evaluateList.size()>0){
            List<Long> rids = new ArrayList<>();
            for(EhmInfoEvaluate newsRates:evaluateList){
                rids.add(newsRates.getUserId());
            }
            EhmResidentQo ehmResidentQo = new EhmResidentQo();
            ehmResidentQo.setInIds(rids);
            List<EhmResidentVo> residents = ehmResidentApi.list(ehmResidentQo);
            Map<Long,EhmResidentVo> residentVoMap = new HashMap<>();
            for (EhmResidentVo residentVo:residents){
                residentVoMap.put(residentVo.getId(),residentVo);
            }
            for (EhmInfoEvaluate infoEvaluate:evaluateList){
                if(residentVoMap.containsKey(infoEvaluate.getUserId())){
                    infoEvaluate.setName(residentVoMap.get(infoEvaluate.getUserId()).getNickname());
                    infoEvaluate.setAvatar(residentVoMap.get(infoEvaluate.getUserId()).getImage());
                }
            }

        }

        long iTotalRecords = new PageInfo(evaluateList).getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("response", new Response(0));
        retMsg.put("data", evaluateList);
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }
}